<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-03-05 14:13:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-03-18 16:16:49
// +-----------------------------------------------------------------------


/**
 * 人员控制器
 * @authors 琥珀 (317559272@qq.com)
 */
namespace Admin\Controller;
use Think\Controller;
Class UserController extends AdminController
{
    public function index() {
        $model = M('Admin');
        $searchname = I('searchname');
        if ($searchname) {
            $map['uid|name|author'] = array(intval($searchname), array('like', '%' . $searchname . '%'), array('like', '%' . $searchname . '%'), '_multi' => true);
        }
        $list = $this->lists($model, $map);
        foreach ($list as $key => $value) {
            $hospital_name = get_hospital_name($value['deafult_hospital_id']);
            $list[$key]['hospital_name'] = $hospital_name ? $hospital_name : null;
            $depart_name = get_depart_name($value['depart_id']);
            $list[$key]['depart_name'] = $depart_name ? $depart_name : null;

            //获取多医院名称
            $hospital_names = implode(",", get_hospital_name(explode(",", $value['hospital_id'])));
            $list[$key]['hospital_names'] = $hospital_names ? $hospital_names : null;
        }
        $this->assign('list', $list);
        $this->assign('searchname', $searchname);
        $this->display();
    }
    public function add() {
        if (IS_POST) {
            $User = D('User');
            $data = I('post.');
            if (!$data['deafult_hospital_id']) {
                $this->error('没有选择默认医院');
            }
            if (!$data['hospital_id']) {
                $this->error('没有选择所属医院');
            }
            if (is_array($data['hospital_id'])) {
                if (!in_array($data['deafult_hospital_id'], $data['hospital_id'])) {
                    $this->error('默认医院不在所属医院内');
                }
            } elseif ($data['deafult_hospital_id'] !== $data['hospital_id']) {
                $this->error('默认医院不在所属医院内');
            }
            if (is_array($data['hospital_id'])) {
                $data['hospital_id'] = implode(",", $data['hospital_id']);
            }
            if (!$User->create($data)) {
                $this->error(($User->getError()));
            } else {
                $rs = $User->add();
            }
            if ($rs !== false) {
                $this->success('新增成功!');
            } else {
                $this->error('新增失败!');
            }
        } else {
            $this->assign('hospital', get_hospital_list());
            $this->assign('depart', get_depart_list());
            $this->display();
        }
    }
    public function edit($uid = null) {
        if (IS_POST) {
            $User = D('User');
            $data = I('post.');
            if (!$data['deafult_hospital_id']) {
                $this->error('没有选择默认医院');
            }
            if (!$data['hospital_id']) {
                $this->error('没有选择所属医院');
            }
            if (is_array($data['hospital_id'])) {
                if (!in_array($data['deafult_hospital_id'], $data['hospital_id'])) {
                    $this->error('默认医院不在所属医院内');
                }
            } elseif ($data['deafult_hospital_id'] !== $data['hospital_id']) {
                $this->error('默认医院不在所属医院内1');
            }
            if (is_array($data['hospital_id'])) {
                $data['hospital_id'] = implode(",", $data['hospital_id']);
            }
            if (!$User->create($data)) {
                $this->error(($User->getError()));
            } else {
                $rs = $User->save();
            }
            if ($rs !== false) {
                $this->success('账号修改成功!如有修改医院请重新进行数据授权');
            } else {
                $this->error('修改失败!');
            }
        } else {
            if (array_intersect(C('USER_ADMINISTRATOR'), array($id))) {
                $this->error("不允许对超级管理员执行该操作!");
            } else {
                $uid = I('uid');
                empty($uid) && $this->error('参数错误！');
                $map['uid'] = $uid;
                $data = M('Admin')->where($map)->find();
                $hospital_ids = explode(",", $data['hospital_id']);
                $this->assign('data', $data);
                $maps['status'] = 1;
                $hospital = M('Hospital')->where($maps)->select();
                $rs = array();
                foreach ($hospital as $key => $value) {
                    if (in_array($value['id'], $hospital_ids)) {
                        $value['select'] = 'selected="selected"';
                    } else {
                        $value['select'] = '';
                    }
                    $rs[$key] = $value;
                }
                $depart_list = get_depart_list();
                foreach ($depart_list as $key => $value) {
                    if ($value['id'] == $data['depart_id']) {
                        $value['select'] = 'selected="selected"';
                    } else {
                        $value['select'] = '';
                    }
                    $depart_list[$key] = $value;
                }
                $this->assign('hospital', $rs);
                $this->assign('depart', $depart_list);
                $this->display();
            }
        }
    }
    public function del($ids = null) {
        empty($ids) && $this->error('参数错误！');
        if (is_array($ids)) {
            $ids = implode(',', $ids);
            if (array_intersect(C('USER_ADMINISTRATOR'), $ids)) {
                $this->error("不允许对超级管理员执行该操作!");
            }
        } else {
            $ids = $ids;
            if (array_intersect(C('USER_ADMINISTRATOR'), array($ids))) {
                $this->error("不允许对超级管理员执行该操作!");
            }
        }
        $map['uid'] = array('in', $ids);
        $res = M('Admin')->where($map)->delete();
        if ($res !== false) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    //修改密码
    public function updatePassword() {
        if (IS_POST) {
            $Admin = M('Admin');
            $data = I('post.');
            $uid = session('user_auth') ['uid'];
            $map['uid'] = $uid;
            $user = $Admin->where($map)->find();
            if (!$user) {
                $this->error('当前用户不存在!');
            }
            if ($user['password'] !== md5($data['oldpassword'])) {
                $this->error('原始密码错误');
            }
            if ($data['newpassword'] !== $data['repassword']) {
                $this->error('两次输入密码不同');
            }
            $datas['uid'] = $user['uid'];
            $datas['password'] = md5($data['newpassword']);
            $rs = $Admin->save($datas);
            if (!$rs) {
                $this->error('修改失败');
            } else {
                $this->success('修改成功');
            }
        } else {
            $userid = session('user_auth') ['uid'];
            $this->assign('userid', $userid);
            $this->display();
        }
    }
    public function userinfo() {
        $this->display();
    }
    public function userData() {
        if (IS_POST) {
            $data = I('post.');
            if (empty($data['data_id'])) {
                $this->error('请选择数据!');
            }
            if (is_array($data['data_id'])) {
                $data['data_id'] = implode(",", $data['data_id']);
            }
            $model = M('Admin');
            if (false === $model->create($data)) {
                $this->error($model->getError());
            }
            $rs = $model->save();
            if (!$rs) {
                $this->error('数据授权失败');
            } else {
                $this->success('数据授权成功');
            }
        } else {
            $uid = I('uid');
            empty($uid) && $this->error('参数错误！');
            $map['uid'] = $uid;
            $data = M('Admin')->where($map)->find();
            if (!$data) {
                $this->error('当前用户不存在或者已被管理员禁用!');
            }
            $hospital_ids = explode(",", $data['hospital_id']);
            $maps['hospital_id'] = array('IN', $hospital_ids);
            $hospital = M('Hospital')->select();
            $department = M('Department')->where($maps)->select();
            foreach ($department as $key => $value) {
                foreach ($hospital as $v) {
                    if ($value['hospital_id'] == $v['id']) {
                        $department[$key]['hospital_name'] = $v['name'];
                    }
                }
            }
            $data_id = M('Admin')->where($map)->getfield('data_id');
            if ($data_id) {
                $data_id = explode(",", $data_id);
                foreach ($department as $key => $value) {
                   if (in_array($value['id'],$data_id)) {
                      $department[$key]['selected'] = "checked";
                   }
                }
            }
            $this->assign('uid', $uid);
            $this->assign('department', $department);
            $this->display();
        }
    }
}
?>